********************************************************************************
** 	TITLE:		UK59_results                                                  ** 
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       1918-2019election_results_by_pcon         		              **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close
log using "UK59_results.smcl", replace

* Import the dataset

import excel "1918-2019election_results_by_pcon.xlsx", cellrange(B5:X634) sheet(1959) clear

* Reshape

drop G J M P S V

rename B id
rename C constituency_name
rename D county
rename E region
rename F electorate
rename H votes1
rename I share1
rename K votes2
rename L share2
rename N votes3
rename O share3
rename Q votes4
rename R share4
rename T votes5
rename U share5
rename W totalvotes
rename X turnout

reshape long votes share, i(id) j(party_code)  

gen party_name = " "

replace party_name = "Conservative" if party_code == 1 
replace party_name = "Labour" if party_code == 2
replace party_name = "Liberal" if party_code == 3
replace party_name = "Nationalists" if party_code == 4
replace party_name = "Other" if party_code == 5

drop party_code

gen party_code = .

replace party_code = 1 if party_name == "Conservative"
replace party_code = 2 if party_name == "Labour"
replace party_code = 3 if party_name == "Liberal"
replace party_code = 4 if party_name == "Nationalists"
replace party_code = 5 if party_name == "Other"
replace party_code = -9999 if region == "Northern Ireland"
 
* Generate rank of parties

gsort constituency_name -votes
bysort constituency_name : gen rank = _n

gen first = party_name if rank == 1
gen second = party_name if rank == 2
gen third = party_name if rank == 3

bysort constituency_name : gen winner59 = first[1]
bysort constituency_name : gen runnerup59 = second[2]
bysort constituency_name : gen thirdplace59 = third[3]

* Generate margins of victory

bysort constituency_name : gen margin = (share[1] - share[2])*100

* Generate effective number of electoral parties

gen sqrvotepr = (share)^2

bysort constituency_name : egen sumsqrvotepr = sum(sqrvotepr)

gen enep = 1/sumsqrvotepr

* Labelling parties

replace party_name = "1" if party_name == "Conservative"
replace party_name = "2" if party_name == "Labour"
replace party_name = "3" if party_name == "Liberal"
replace party_name = "4" if party_name == "Nationalists"
replace party_name = "5" if party_name == "Other"

destring (party_name), replace

replace winner59 = "1" if winner59 == "Conservative"
replace winner59 = "2" if winner59 == "Labour"
replace winner59 = "3" if winner59 == "Liberal"
replace winner59 = "4" if winner59 == "Nationalists"
replace winner59 = "5" if winner59 == "Other"

destring (winner59), replace

replace runnerup59 = "1" if runnerup59 == "Conservative"
replace runnerup59 = "2" if runnerup59 == "Labour"
replace runnerup59 = "3" if runnerup59 == "Liberal"
replace runnerup59 = "4" if runnerup59 == "Nationalists"
replace runnerup59 = "5" if runnerup59 == "Other"

destring (runnerup59), replace

replace thirdplace59 = "1" if thirdplace59 == "Conservative"
replace thirdplace59 = "2" if thirdplace59 == "Labour"
replace thirdplace59 = "3" if thirdplace59 == "Liberal"
replace thirdplace59 = "4" if thirdplace59 == "Nationalists"
replace thirdplace59 = "5" if thirdplace59 == "Other"

destring (thirdplace59), replace

label define party 1 "Conservative" 2 "Labour" 3 "Liberal" 4 "Nationalists" 5 "Other"
label values party_name winner59 runnerup59 thirdplace59 party
 
* Selecting, ordering, and sorting variables

keep id constituency_name party_name rank votes share margin enep winner59 runnerup59 thirdplace59

order id constituency_name party_name rank votes share margin enep winner59 runnerup59 thirdplace59

sort id rank

drop if rank > 3

* Clean district names

replace constituency_name = upper(constituency_name)

* Drop duplicates

duplicates tag id, gen(dup)
duplicates drop id, force
drop dup

* Create election variable

gen election59 = "1959 British general election"

* Saving file

save UK59_results, replace

log close